<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>练习1-ES6/7常用语法介绍</title>
    </head>

    <body>
        <pre>
            <code>
                    // 练习1：请将下面的代码用ES6的写法进行改写

                    // 接口:
                    //   学生选修课选择情况表：https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses_selection/
                    //   选修课表：https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses/
                    'use strict';

                    ;
                    (function () {
                        var API = {
                            COURSES_SELECTION: 'https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses_selection/',
                            COURSES: 'https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses/'
                        };

                        var app = {};

                        /**
                         *  [学生选修课选择情况表数据]
                         *  @param {Function} successCb [请求成功后的回调函数]
                         *  @param {Function} failCb [请求失败后的回调函数]
                         */
                        app.getCoursesSelection = function (successCb, failCb) {
                            $.ajax({
                                url: API.COURSES_SELECTION,
                                type: 'GET',
                                success: function (res) {
                                    if (typeof successCb === 'function') {
                                        successCb(res);
                                    }
                                },
                                error: function (err) {
                                    if (typeof failCb === 'function') {
                                        failCb(err);
                                    }
                                }
                            })
                        }

                        /**
                         *  [获取选修课表数据]
                         *   @param {Function} successCb [请求成功后的回调函数]
                         *   @param {Function} failCb [请求失败后的回调函数]
                         */
                        app.getCourses = function (successCb, failCb) {
                            $.ajax({
                                url: API.COURSES,
                                type: 'GET',
                                success: function (res) {
                                    if (typeof successCb === 'function') {
                                        successCb(res);
                                    }
                                },
                                error: function (err) {
                                    if (typeof failCb === 'function') {
                                        failCb(err);
                                    }
                                }
                            })
                        }

                        /**
                         *  [创建HTML模板]
                         *  @param {Array} data [数据]
                         *  @return {String} html [HTML模板字符串]
                         */
                        app.createTemplate = function (data) {
                            var html = '';
                            if (data instanceof Array) {
                                html += '&lt;table&gt;';
                                html += '   &lt;thead&gt;';
                                html += '       &lt;tr&gt;';
                                html += '           &lt;th&gt;学号&lt;/th&gt;';
                                html += '           &lt;th&gt;姓名&lt;/th&gt;';
                                html += '           &lt;th&gt;年龄&lt;/th&gt;';
                                html += '           &lt;th&gt;所选课程&lt;/th&gt;';
                                html += '       &lt;/tr&gt;';
                                html += '   &lt;/thead&gt;';
                                html += '   &lt;tbody&gt;';

                                data.map(function (item) {
                                    html += '   &lt;tr&gt;';
                                    html += '       &lt;td&gt;' + item.number + '&lt;/td&gt;';
                                    html += '       &lt;td&gt;' + item.name + '&lt;/td&gt;';
                                    html += '       &lt;td&gt;' + item.age + '&lt;/td&gt;';
                                    html += '       &lt;td&gt;' + item.subject + '&lt;/td&gt;';
                                    html += '   &lt;/tr&gt;';
                                })

                                html += '   &lt;/tbody&gt;';
                                html += '&lt;/table&gt;';
                            }

                            return html;
                        }

                        /**
                         *  [初始化类]
                         */
                        app.initClass = function () {
                            /**
                             *  [构造函数Person]
                             *  @param {String} name [姓名]
                             *  @param {Integer} name [年龄]
                             */
                            var Person = function (name, age) {
                                this.name = name;
                                this.age = age;
                            }

                            Person.prototype = {
                                constructor: Person,
                                getName: function () {
                                    return this.name;
                                },
                                getAge: function () {
                                    return this.age;
                                }
                            }

                            /**
                             *  [构造函数Student]
                             *  @param {String} name [姓名]
                             *  @param {Integer} name [年龄]
                             *  @param {String} number [学号]
                             *  @param {String} subject [所选课程]
                             */
                            var Student = function (name, age, number, subject) {
                                Person.call(this, name, age);
                                this.number = number;
                                this.subject = subject;
                            }

                            Student.prototype = new Person();
                            Student.prototype.constructor = Student;
                            Student.prototype.getNumber = function () {
                                return this.number;
                            }
                            Student.prototype.getSubject = function () {
                                return this.subject;
                            }

                            this.Student = Student;
                        }

                        /**
                         *  [数据处理]
                         */
                        app.dataHandler = function () {
                            var _this = this;
                            this.getCoursesSelection(function (res) {
                                var data1 = [].concat(res.data);
                                var result = [];

                                _this.getCourses(function (res) {
                                    data1.map(function (item1) {
                                        res.data.map(function (item2) {
                                            if (item1.subjectId === item2.number) {
                                                item1.subjectName = item2.name;
                                            }
                                        })

                                        return item1;
                                    }).map(function (item1) {
                                        result.push(new _this.Student(item1.name, item1.age, item1.number, item1.subjectName));
                                    })

                                    $('#result').html(_this.createTemplate(result));
                                })
                            })
                        }

                        /**
                         *  [应用初始化]
                         */
                        app.init = function () {
                            this.initClass();
                            this.dataHandler();
                        }

                        app.init();
                    })();
            </code>
        </pre>
        <div id="result"></div>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/libs/jquery-1.8.3.min.js"></script>
        <script src="scripts/p_1.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>

</html>